package com.lill.support.aspect;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lill.system.model.SysUserLoginAppEntity;
import com.lill.system.service.ICommonRedisService;

@Component
@Aspect
public class OnceSubmitAopAspect {
	
	protected static Logger logger = LoggerFactory.getLogger(OnceSubmitAopAspect.class);
	
	
	private final ObjectMapper mapper;
	@Autowired
	private ICommonRedisService commonRedisService;
	
	@Autowired
	public OnceSubmitAopAspect(ObjectMapper mapper) {
		this.mapper = mapper;
	}
 
    @Around("@annotation(com.lill.support.annotation.OnceSubmit)")
    public Object arround(ProceedingJoinPoint pjp) {
        try {
        	String userId=null;
        	String token=null;
        	StringBuffer params=new StringBuffer("md5");
        	for (Object object : pjp.getArgs()) {
        		if (object instanceof MultipartFile) {
        			MultipartFile file=(MultipartFile) object;
        			params.append(file.getName());
        			continue;
    			}
        		
        		if (object instanceof MultipartFile[]) {
        			MultipartFile[] files=(MultipartFile[]) object;
        			for (MultipartFile file : files) {
        				params.append(file.getName());
					}
        			continue;
        		}
        		
        		if(object instanceof SysUserLoginAppEntity){
        			SysUserLoginAppEntity objAppEntity=(SysUserLoginAppEntity) object;
        			userId=objAppEntity.getId();
        			token=objAppEntity.getToken();
        			continue;
        		}
        		params.append(mapper.writeValueAsString(object));
        	}
        	
        	if(StringUtils.isNotEmpty(userId)&&StringUtils.isNotEmpty(token)){
        		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            	HttpServletRequest request = attributes.getRequest();
            	String uri=request.getRequestURI();
        		
            	String key="NoRepeatSubmit:"+token+uri+DigestUtils.md5Hex(params.toString()).toUpperCase();
            	logger.info("--------key:"+key);
            	
	            if (commonRedisService.getIntegerValueByKey(key) == null) {// 如果缓存中有这个url视为重复提交
	            	commonRedisService.setKeyStringValueIntegerWithTime(key, 0, 5, TimeUnit.SECONDS);
	            	return pjp.proceed();
	            }else{
	            	return null;
	            }
        	}else{
        		return null;
        	}
        } catch (Throwable e) {
        	logger.error(e.getMessage(), e);
        	return null;
        }
    }

}
